package com.wenli.lt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wenli.lt.entity.Goods;
import com.wenli.lt.entity.Home;
import com.wenli.lt.entity.Img;
import com.wenli.lt.entity.po.HomePage;
import com.wenli.lt.mapper.GoodsMapper;
import com.wenli.lt.mapper.HomeMapper;
import com.wenli.lt.mapper.ImgMapper;
import com.wenli.lt.service.IHomeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lt
 * @since 2021-04-01
 */
@Service
public class HomeServiceImpl extends ServiceImpl<HomeMapper, Home> implements IHomeService {

    @Autowired
    HomeMapper homeMapper;

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    ImgMapper imgMapper;

    @Override
    public boolean updateHome(String goodsId, String homeId) {
        Home home = homeMapper.selectById(homeId);
//        Goods goods = goodsMapper.selectById(goodsId);
        QueryWrapper<Img> queryWrapper = new QueryWrapper();
        queryWrapper.eq("goodsId",goodsId);
        queryWrapper.select().orderByAsc("createTime");
        List<Img> imgs = imgMapper.selectList(queryWrapper);
        if (!ObjectUtils.isNull(imgs)){
            home.setImgUrl(imgs.get(0).getImageUrl());
        }
        home.setGoodsId(goodsId);
        if (!StringUtils.isBlank(homeId) ){
            return 1 == homeMapper.updateById(home);

        }else {
            return false;
        }
    }

    @Override
    public Map selectHome(Integer classification) {
        QueryWrapper<Home> queryWrapper = new QueryWrapper();
        queryWrapper.eq("classification",classification);
        queryWrapper.select().orderByAsc("ranking");
        List<HomePage> homes = homeMapper.selectHomeList( classification);
        Map map =  new HashMap();
        List list = new ArrayList();
        List list1 = new ArrayList();
        List list2 = new ArrayList();
        List list3 = new ArrayList();
        List list4 = new ArrayList();
        List list5 = new ArrayList();
        if (classification == 0  || classification == 2 || classification == 4){
        for (int i = 0; i < homes.size(); i++) {
            if (i < 3){
                list.add(homes.get(i));
            }else if (i >= 3 && i < 6){
                list1.add(homes.get(i));

            }else {
                list2.add(homes.get(i));
            }
        }
        }else if (classification == 1){
            for (int i = 0; i < homes.size(); i++) {
                if (i < 3){
                    list.add(homes.get(i));
                }else if (i >= 3 && i < 7){
                    list1.add(homes.get(i));

                }else {
                    list2.add(homes.get(i));
                }
            }
        }else  if (classification == 5){
            for (int i = 0; i < homes.size(); i++) {
                if (i < 3){
                    list.add(homes.get(i));
                }else if (i >= 3 && i < 8){
                    list1.add(homes.get(i));

                }else {
                    list2.add(homes.get(i));
                }
            }
        }else if (classification == 3){
            for (int i = 0; i < homes.size(); i++) {
                if (i < 4){
                    list.add(homes.get(i));
                }else{
                    list1.add(homes.get(i));

                }
            }
        }else {
            for (int i = 0; i < homes.size(); i++) {
                if (i < 3){
                    list.add(homes.get(i));
                }else if (i >= 3 && i < 14){
                    list1.add(homes.get(i));

                }else if (i >= 14 && i < 18){
                    list2.add(homes.get(i));
                }else if (i >= 18 && i < 22){
                    list3.add(homes.get(i));
                }else if (i >= 22 && i < 25){
                    list4.add(homes.get(i));
                }else {
                    list5.add(homes.get(i));
                }
            }
        }
        if (classification != 6){
            map.put("one",list);
            map.put("two",list1);
            if (classification != 3){
                map.put("three",list2);
            }
        }else {
            map.put("clothing",list);
            map.put("makeup",list1);
            map.put("fruniture",list2);
            map.put("book",list3);
            map.put("life",list4);
            map.put("phone",list5);
        }

        return map;
    }

    @Override
    public Map selectHomePage() {
        Map map = new HashMap();
        map.put("home",selectHome(6));
        map.put("clothing",selectHome(0));
        map.put("makeup",selectHome(1));
        map.put("fruniture",selectHome(2));
        map.put("book",selectHome(3));
        map.put("life",selectHome(4));
        map.put("phone",selectHome(5));
        return map;
    }


}
